package com.hxzy.controller.api;

import com.hxzy.common.TZXMConst;
import com.hxzy.common.vo.R;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 图片验证码的控制器
 */
@Api(tags = "图像验证码")
@RestController
@RequestMapping(value = "/api/captcha")
public class CaptchaController {

     @Autowired
     private RedisTemplate  redisTemplate;


     /**
      * 生成png格式
      * @param width
      * @param height
      * @param len
      * @return
      */
     @ApiOperation(value = "png图片验证码")
     @ApiImplicitParams(value = {
             @ApiImplicitParam(name = "width",value = "图片宽度",defaultValue = "150"),
             @ApiImplicitParam(name = "height",value = "图片高度",defaultValue = "50"),
             @ApiImplicitParam(name = "len",value = "验证码长度",defaultValue = "4"),
     })
     @GetMapping(value = "/png")
     public R   png(@RequestParam(value = "width",defaultValue = "150") int width,
                    @RequestParam(value = "height",defaultValue = "50") int height,
                    @RequestParam(value = "len",defaultValue = "4") int len){
          //1、生成验证码
          SpecCaptcha captcha = new SpecCaptcha(width, height);
          captcha.setLen(len);  //验证码长度
          String value=captcha.text();  // 获取验证码的字符
          // 把图片转换成base64加密的图片字符串
          String imgSrc=captcha.toBase64();

          //2、存放到redis中
          String uuid=UUID.randomUUID().toString();
          String redisKey= TZXMConst.REDIS_CAPTCHA_KEY+uuid;
          this.redisTemplate.opsForValue().set(redisKey,value,TZXMConst.REDIS_CAPTCHA_EXPIRED, TimeUnit.MINUTES);

          //2、创建实体并存储到数据库
//          Captcha cp=new Captcha();
//          cp.setUuid(UUID.randomUUID().toString());
//          cp.setValue(value);
//          //5分钟之内有效
//          long expired=System.currentTimeMillis() + 1000L*60*5;
//          cp.setExpiredTime(new Date(expired));
//          this.captchaService.save(cp);

          //3、把数据返回给前端
          Map<String,Object> data=new HashMap<>();
          data.put("uuid", uuid);
          data.put("img", imgSrc);
          return R.success(data);
     }

     /**
      * 生成gif格式
      * @param width
      * @param height
      * @param len
      * @return
      */
     @ApiOperation(value = "gif图片验证码")
     @ApiImplicitParams(value = {
             @ApiImplicitParam(name = "width",value = "图片宽度",defaultValue = "150"),
             @ApiImplicitParam(name = "height",value = "图片高度",defaultValue = "50"),
             @ApiImplicitParam(name = "len",value = "验证码长度",defaultValue = "4"),
     })
     @GetMapping(value = "/gif")
     public R   gif(@RequestParam(value = "width",defaultValue = "150") int width,
                    @RequestParam(value = "height",defaultValue = "50") int height,
                    @RequestParam(value = "len",defaultValue = "4") int len){
          //1、生成验证码
          GifCaptcha captcha = new GifCaptcha(width, height);
          captcha.setLen(len);  //验证码长度
          String value=captcha.text();  // 获取验证码的字符
          // 把图片转换成base64加密的图片字符串
          String imgSrc=captcha.toBase64();

          String uuid=UUID.randomUUID().toString();
          String redisKey= TZXMConst.REDIS_CAPTCHA_KEY+uuid;
          this.redisTemplate.opsForValue().set(redisKey,value,TZXMConst.REDIS_CAPTCHA_EXPIRED, TimeUnit.MINUTES);

          //3、把数据返回给前端
          Map<String,Object> data=new HashMap<>();
          data.put("uuid", uuid);
          data.put("img", imgSrc);
          return R.success(data);
     }

     /**
      * 生成数学运算格式
      * @param width
      * @param height
      * @param len
      * @return
      */
     @ApiOperation(value = "数学运算验证码")
     @ApiImplicitParams(value = {
             @ApiImplicitParam(name = "width",value = "图片宽度",defaultValue = "150"),
             @ApiImplicitParam(name = "height",value = "图片高度",defaultValue = "50"),
             @ApiImplicitParam(name = "len",value = "数学运算个数",defaultValue = "2"),
     })
     @GetMapping(value = "/airth")
     public R   airth(@RequestParam(value = "width",defaultValue = "150") int width,
                    @RequestParam(value = "height",defaultValue = "50") int height,
                    @RequestParam(value = "len",defaultValue = "2") int len){
          //1、生成验证码
          ArithmeticCaptcha captcha = new ArithmeticCaptcha(width, height);
          captcha.setLen(len);  //验证码长度
          String value=captcha.text();  // 获取验证码的字符
          // 把图片转换成base64加密的图片字符串
          String imgSrc=captcha.toBase64();

          String uuid=UUID.randomUUID().toString();
          String redisKey= TZXMConst.REDIS_CAPTCHA_KEY+uuid;
          this.redisTemplate.opsForValue().set(redisKey,value,TZXMConst.REDIS_CAPTCHA_EXPIRED, TimeUnit.MINUTES);

          //3、把数据返回给前端
          Map<String,Object> data=new HashMap<>();
           data.put("uuid", uuid);
          data.put("img", imgSrc);
          return R.success(data);
     }

}
